﻿using System.Text;
using System.Xml;

namespace AT.Data.Core.ExtendData
{
    public class ExtendDictionary : Dictionary<string, ValueItem>
    {
        private const string Root = "datax";
        private string _ns;
        private string _version;

        public ExtendDictionary()
        {
           
        }

        public ExtendDictionary(string xml) : this()
        {
            this.LoadData(xml);
        }

        public void AddText(string key, string val)
        {
            if (!this.ContainsKey(key))
                this.Add(key, new ValueItem(ValueItemEnum.text.ToString(), val));
            else
                this[key] = new ValueItem(ValueItemEnum.text.ToString(), val);
        }

        public string ToXML()
        {
            var xbuilder = new StringBuilder();
            xbuilder.Append($"<{Root} ns=\"{this._ns}\" v=\"{this._version}\">");

            foreach (var item in this)
            {
                xbuilder.Append($"<i t=\"{item.Value.Type}\" n=\"{item.Key}\">");
                if(item.Value.Type == ValueItemEnum.text.ToString() || item.Value.Type == ValueItemEnum.html.ToString())
                    xbuilder.Append(Web.HtmlEncode(item.Value.Val));
                xbuilder.Append($"</i>");
            }
            xbuilder.Append($"</{Root}>");
            return xbuilder.ToString();
        }

        private void LoadData(string xml)
        {
            try
            {
                if (string.IsNullOrEmpty(xml)) return;

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);
                if (xmlDoc == null) return;

                //root
                var root = xmlDoc.SelectSingleNode(Root);
                if (root == null) return;
                for (var i = 0; i < root.Attributes?.Count; i++)
                {
                    if (root.Attributes[i].Name == "ns") this._ns = root.Attributes[i].Value;
                    if (root.Attributes[i].Name == "v") this._version = root.Attributes[i].Value;
                }

                //item
                foreach (XmlNode node in xmlDoc.SelectNodes($"{Root}//i"))
                {
                    var key = "";
                    var type = ValueItemEnum.text.ToString();
                    var val = Web.HtmlDecode(node.InnerText);
                    for (var i = 0; i < node.Attributes?.Count; i++)
                    {
                        if (node.Attributes[i].Name == "n") key = node.Attributes[i].Value;
                        if (node.Attributes[i].Name == "t") type = node.Attributes[i].Value;
                    }
                    if(!string.IsNullOrEmpty(key))
                    {
                        if (!this.ContainsKey(key))
                            this.Add(key, new ValueItem(type, val));
                        else
                            this[key] = new ValueItem(type, val);
                    }
                }
            }
            catch { }
        }
    }
}
